package com.rytong.tools.ui;

import java.util.HashMap;

import android.app.Activity;

import com.rytong.tools.ui.LPPieChart.MyLPPieChart;
import com.rytong.tools.utils.Utils;

public class LPSector extends Component {

    public int color;// 扇形背景色

    // start角度
    float startAngle_;

    // 跨越的角度
    float sweepAngle_;

    public float fontSize_ = 14;

    public int fontColor_;

    private Activity activity_;

    private static LPPieChart pieChart;

    @Override
    public void setTag(String name) {
    }

    @Override
    public String getTag() {
        return "";
    }

    @Override
    public void initRealView(Activity activity, String nodeValue) {
        super.initRealView(activity, nodeValue);
        activity_ = activity;
    }

    @Override
    public void mouldH() throws Exception {
        super.mouldH();
        if (parentComponent_ != null) {
            int size = parentComponent_.childrenList_.size();
            Component child;
            String piechartName = null;
            String childName = null;
            int childCount = parentComponent_.getChildCount();
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < childCount; i++) {
                child = parentComponent_.getChildAt(i);
                if (child instanceof LPPieChart) {
                    childName = (String) child.getPropertyByName("name");
                    buffer.append(childName + ",");
                }
            }
            for (int i = 0; i < size; i++) {
                child = parentComponent_.childrenList_.get(i);
                childName = (String) child.getPropertyByName("name");
                if (childName != null && buffer.length() > 0 && buffer.indexOf(childName) > -1) {
                    break;
                }
                if (child instanceof LPSector) {
                    if (parentComponent_.pieChart_ == null) {
                        parentComponent_.pieChart_ = new LPPieChart();
                        parentComponent_.pieChart_.isAfterBr_ = child.isAfterBr_;
                        parentComponent_.pieChart_.attributes_ = child.attributes_;
                        parentComponent_.pieChart_.property_ = child.property_;
                        parentComponent_.pieChart_.setCssStyle(child.cssStyle());
                        parentComponent_.pieChart_.sectorList_.add((LPSector) child);
                        pieChart = parentComponent_.pieChart_;
                    } else {
                        piechartName = (String) parentComponent_.pieChart_.getPropertyByName("name");
                        if (piechartName != null && childName != null && piechartName.equals(childName)) {
                            parentComponent_.pieChart_.sectorList_.add((LPSector) child);
                        } else {
                            addPieChartToLayout();
                        }
                    }
                }
                if (parentComponent_.pieChart_ != null && i == size - 1) {
                    addPieChartToLayout();
                }
            }
        }
        if (null != cssStyle_) {
            // 设置字体大小
            String fontSizeStr = (String) cssStyle_.getProperty(CssStyle.FONTSIZE);
            if (null != fontSizeStr && !fontSizeStr.equals("")) {
                int fontSize = Integer.parseInt(fixedPX(fontSizeStr));
                this.fontSize_ = Utils.getFontSize(fontSize);
            }

            // 设置字体颜色
            this.fontColor_ = fgColor();
            // 设置扇形背景色
            this.color = bgColor();
        }
    }

 
    
    /**
     * @throws Exception
     * 将饼图添加到父布局中
     */
    private void addPieChartToLayout() throws Exception {
        parentComponent_.pieChart_.initRealView(activity_, null);
        parentComponent_.pieChart_.setTag("pie");
        parentComponent_.addView(parentComponent_.pieChart_);
        parentComponent_.pieChart_.mouldW();
        parentComponent_.pieChart_.adjustXY();
        parentComponent_.pieChart_.mouldH();
        Component.VWIDGETARRAY.add(parentComponent_.pieChart_);
        parentComponent_.pieChart_ = null;
    }
    
//Override
    public void setPropertyByName(String key, String value) {
        if(key != null && key.equals("percent")){
            if (null == property_)
                property_ = new HashMap<String, String>();
            property_.put(key, value);
            try {
                ((MyLPPieChart) pieChart.realView_).setupSectors();
                pieChart.invalidate();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.setPropertyByName(key, value);
    }
}
